package com.example.topicUI;

import com.example.wifidirectclient.R;
import com.example.wifidirectclientMethods.ClientService;
import com.example.wifidirectclientMethods.DirectClientService;

import android.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class TopicFragment extends Fragment {
	
	public static Handler topicHandler;
	private View mView;
	private TextView ans;
	String strAns = "";
	String multiAns = "";
	private Button sendAns;
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);	
		Log.d("WifiDirectClient", "Fragment created");
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle data){
		
		mView = getAns_suggest();
		return mView;
	}
	
	
	
	private View getAns_suggest(){
		LayoutInflater inflater = getActivity().getLayoutInflater();
		View view = inflater.inflate(R.layout.suggest_fankui, null);
		ans = (TextView)view.findViewById(R.id.ans_text);		
		sendAns = (Button)view.findViewById(R.id.send_ans);
		sendAns.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				Message msg = new Message();
				msg.what = 0x234; 
				msg.obj = strAns + multiAns;
				ClientService.revHandler.sendMessage(msg);
				Log.d("WifiDirectClient", "send answer:"+msg.obj.toString());
				sendAns.setVisibility(View.GONE);
			}});
		
		topicHandler = new Handler(){
			@Override
			public void handleMessage(Message msg){
				if(msg.what == 0x11){
					strAns ="Single:" + msg.obj.toString();
					ans.setText(strAns + '\n' + multiAns);
					Log.d("WifiDirectClient", "rev:"+msg.obj.toString());
				}else if(msg.what == 0x12){
					multiAns = "Multi:" + msg.obj.toString();
					ans.setText(strAns + '\n' + multiAns);
				}else if(msg.what == 0x13){
					ans.append("\n"+"right answer:"+'\n'+msg.obj);
				}
			}
		};
		return view;
	}

}
